﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebMVC.Controllers
{
    public class ChiNhanhController : Controller
    {
        databaseDataContext db = new databaseDataContext();
        //
        // GET: /ChiNhanh/
        [Authorize(Roles = "0")]
        public ActionResult Index()
        {
            ViewData["Items"] = from h in db.ChiNhanhs
                                select h;
            return View();
        }

        //
        // GET: /ChiNhanh/Details/5
        [Authorize(Roles = "0")]
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /ChiNhanh/Create
        [Authorize(Roles = "0")]
        public ActionResult Create()
        {
            IEnumerable<SelectListItem> listKhuVuc = db.KhuVucs.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            IEnumerable<SelectListItem> listThanhVien = db.ThanhViens.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten_dang_nhap.ToString()
            });

            ViewData["ListKhuVuc"] = listKhuVuc;
            ViewData["ListThanhVIen"] = listThanhVien;
            return View();
        } 

        //
        // POST: /ChiNhanh/Create
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Create(ChiNhanh chinhanh)
        {
            try
            {
                // TODO: Add insert logic here
                chinhanh.Ngay_cap_nhat_cuoi = DateTime.Now;
                db.ChiNhanhs.InsertOnSubmit(chinhanh);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                IEnumerable<SelectListItem> listKhuVuc = db.KhuVucs.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                IEnumerable<SelectListItem> listThanhVien = db.ThanhViens.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten_dang_nhap.ToString()
                });

                ViewData["ListKhuVuc"] = listKhuVuc;
                ViewData["ListThanhVIen"] = listThanhVien;
                return View();
            }
        }
        
        //
        // GET: /ChiNhanh/Edit/5
        [Authorize(Roles = "0")]
        public ActionResult Edit(int id)
        {
            ChiNhanh model = (from m in db.ChiNhanhs
                             where m.ID == id
                             select m).SingleOrDefault();
            IEnumerable<SelectListItem> listKhuVuc = db.KhuVucs.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            IEnumerable<SelectListItem> listThanhVien = db.ThanhViens.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten_dang_nhap.ToString()
            });

            ViewData["ListKhuVuc"] = listKhuVuc;
            ViewData["ListThanhVIen"] = listThanhVien;
            return View(model);
        }

        //
        // POST: /ChiNhanh/Edit/5
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Edit(int id, ChiNhanh chinhanh)
        {
            ChiNhanh model = (from m in db.ChiNhanhs
                              where m.ID == id
                              select m).SingleOrDefault();
            try
            {
                // TODO: Add update logic here
                model.Ten = chinhanh.Ten;
                model.Ghi_chu = chinhanh.Ghi_chu;
                model.Dia_chi = chinhanh.Dia_chi;
                model.Tel = chinhanh.Tel;
                model.Ma_khu_vuc = chinhanh.Ma_khu_vuc;
                model.Truong_chi_nhanh = chinhanh.Truong_chi_nhanh;
                model.Ngay_cap_nhat_cuoi = DateTime.Now;
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                IEnumerable<SelectListItem> listKhuVuc = db.KhuVucs.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                IEnumerable<SelectListItem> listThanhVien = db.ThanhViens.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten_dang_nhap.ToString()
                });

                ViewData["ListKhuVuc"] = listKhuVuc;
                ViewData["ListThanhVIen"] = listThanhVien;
                return View(model);
            }
        }

        //
        // GET: /ChiNhanh/Delete/5
        [Authorize(Roles = "0")]
        public ActionResult Delete(int id)
        {
            var model = (from h in db.ChiNhanhs
                         where h.ID == id
                         select h).Take(1).SingleOrDefault();
            db.ChiNhanhs.DeleteOnSubmit(model);
            db.SubmitChanges();
            return RedirectToAction("Index");
        }

        //
        // POST: /ChiNhanh/Delete/5
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
